package com.example.boot.express;

import com.example.boot.express.base.QlExpressUtil;
import com.example.boot.express.data.query.OrderQuery;
import com.example.boot.express.operator.JoinBaseOperator;
import com.example.boot.express.operator.JoinOperator;
import com.ql.util.express.DefaultContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExpressService {

    @Autowired
    private QlExpressUtil qlExpressUtil;

    public void callObj2() {
        try {
            DefaultContext<String, Object> context = new DefaultContext<>();

            qlExpressUtil.initRunner(runner -> {
                try {
//                    runner.addOperator("join",new JoinOperator());
//                    runner.replaceOperator("+", new JoinOperator());
                    runner.addFunction("join", new JoinBaseOperator());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            Object r = qlExpressUtil.execute("join(1, 2, 3, 4)", context);
            System.out.println(r);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void callFunction() {
        String statement = "function add(int a,int b){\n"
                + "  return a+b;\n"
                + "};\n"
                + "\n"
                + "function sub(int a,int b){\n"
                + "  return a - b;"
                + "};"
                + ""
                + "a=10;"
                + "return add(a,4) + sub(a,9);";

        DefaultContext<String, Object> context = new DefaultContext<>();
        try {
            Object r = qlExpressUtil.execute(statement, context);
            System.out.println(r);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void callObj() {

        qlExpressUtil.initRunner(runner -> {
            try {
                runner.clearExpressCache();
//                runner.addMacro("计算平均成绩", "(语文+数学+英语)/3.0");
//                runner.addMacro("是否优秀", "计算平均成绩>90");
                runner.addMacro("OrderQuery", "com.example.boot.express.data.query.OrderQuery");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        String statement = "OrderQuery.test();";
        DefaultContext<String, Object> context = new DefaultContext<>();
        try {
            Object r = qlExpressUtil.execute(statement, context);
            System.out.println(r);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void callStrAndObj() {
        String importStr = "import com.example.boot.express.data.query.OrderQuery;";
        String statement = "query = new OrderQuery();query.setCreateDate(new Date());"
                + "query.buyer = 'yutao';"
                + "System.out.println(query.toString());";
        statement = importStr + statement;
        DefaultContext<String, Object> context = new DefaultContext<>();
        try {
            qlExpressUtil.execute(statement, context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
